﻿using Mapster;
using OnWork.Admin.DAL;
using OnWork.Admin.DAL.System;
using OnWork.Admin.Util;
using SqlSugar;

namespace OnWork.Admin.BLL.System
{
    public class SysUserBLL : BaseBLL<SysUser>
    {
        public SysUserBLL(DBManage dBManage) : base(dBManage.System)
        {
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="search">模糊查询</param>
        /// <param name="pagetion"></param>
        /// <returns>分页列表</returns>
        public async Task<List<SysUserDto>> GetPageListAsync(string search, PagetionModel pagetion)
        {
            var temp = base.Queryable();
            if (!string.IsNullOrEmpty(search))
            {
                //temp.Where(x => x.Name.Contains(search) || x.ResponsiblePersonName.Contains(search));
            }

            RefAsync<int> total = 0;//REF和OUT不支持异步,想要真的异步这是最优解
            var list = await temp
                .OrderByDescending(x => x.CreateTime)
                .ToPageListAsync(pagetion.Pagenumber, pagetion.PageSize, total);//ToPageAsync
            var dtoList = list.Adapt<List<SysUserDto>>();
            return dtoList;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<ReturnModel> EntityAddAsync(SysUserDto obj)
        {
            var dtoObj = obj.Adapt<SysUser>();
            var entity = await base.AddAsync(dtoObj);
            if (entity != 0)
            {
                return ReturnModel.AddSuccess();
            }
            else
            {
                return ReturnModel.AddError();
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task<ReturnModel> EntityEditAsync(SysUserDto obj)
        {
            //bool isrepeat = base.GetList(x => x.Name == obj.Name && x.ID != obj.ID).Any();
            //if (isrepeat)
            //{
            //    return ReturnModelHelper.Error("名称已存在");
            //}
            var dtoObj = obj.Adapt<SysUser>();
            int executeAffrows = await base.UpdateAsync(dtoObj);
            if (executeAffrows != 0)
            {
                return ReturnModel.UpdateSuccess();
            }
            else
            {
                return ReturnModel.UpdateError();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ReturnModel> EntityDeleteAsync(string ids)
        {
            var idArr = ids.Split(",").Select(x => long.Parse(x)).ToArray();

            int delCount = await base.DeleteAsync<SysUser>(idArr);
            if (delCount > 0)
            {
                return ReturnModel.DelSuccess();
            }
            else
            {
                return ReturnModel.DelError();
            }
        }
    }
}